<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sitios extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }
    public function index() {
        if($this->session->userdata('barangay_id') != '' || $this->session->userdata('barangay_id') != null) {
            $this->load->model('Sitios_model');
            $data['sitios'] = $this->Sitios_model->getAll();
            // $this->load->view('landmarks_crud');
            $this->load->view('sitios_index', $data);
        } else {
            header('Location: ' . base_url());
        }
    }
    public function crudsitios() {
        if($this->session->userdata('user_resp') == 'admin') {
            if($this->input->post('l_submit')=='y') {
                $this->load->model('Sitios_model');
                if($this->input->post('l_id')=='' || $this->input->post('l_id')==null) {
                    $data['l_res1'] = $this->Sitios_model->insertSitios();
                    print $data['l_res1'];
                } else {
                    $data['l_res2'] = $this->Sitios_model->updateSitios();
                    print $data['l_res2'];
                    //print json_encode($data);
                    //print 'this should have been erred';
                }
            } else {
                if($this->input->get('sitios_id_pk')) {
                    $this->load->model('Sitios_model');
                    $data['sitios'] = $this->Sitios_model->getSitioId($this->input->get('sitio_id_pk'));
                    if(count($data['sitios']) == 0) {
                        $this->load->view('sitios_crud');
                    } else {
                        $this->load->view('sitios_crud', $data);
                    }
                } else {
                    $this->load->view('sitios_crud');
                }
            }
        } else {
            header('Location: ' . base_url() . 'index.php/sitios');
        }
    }
    public function deletesitios() {
        if($this->session->userdata('user_resp') == 'admin') {
            if($this->input->post('sitio_id_pk')) {
                $this->load->model('Sitios_model');
                $result = $this->Sitios_model->deleteSitios($this->input->post('sitio_id_pk'));
                print json_encode($result);
            } else {
                return false;
            }
        } else {
            header('Location: ' . base_url() . 'index.php/sitios');
        }
    }
}